package com.lancer.platform.pay.rest.param;

import com.lancer.platform.pay.utils.ListUtils;
import com.lancer.platform.pay.utils.ListUtilsHook;

import javax.ws.rs.client.WebTarget;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class Path {

    private final List<String> paths;

    private Path(String... paths) {
        this.paths = new ArrayList<>();
        Collections.addAll(this.paths, paths);
    }

    public static Path build(String... paths) {
        Objects.requireNonNull(paths, "Paths are null.");
        return new Path(paths);
    }

    public WebTarget appendToTarget(WebTarget target) {
        WebTarget newTarget = target;
        for (String path : this.paths) {
            newTarget = newTarget.path(path);
        }

        return newTarget;
    }

    @Override
    public String toString() {
//        return this.paths.stream().filter(p -> p != null)
//        .map(p -> {
//            if (!p.startsWith("/")) {
//                return "/" + p;
//            } else {
//                return p;
//            }
//        }).collect(Collectors.joining("", "{Path: ", "}"));

        List<String> r =ListUtils.filter(paths, new ListUtilsHook<String>() {
            @Override
            public boolean test(String s) {
                return s!=null;
            }
        });
        List<String> tmp = new ArrayList<>();
        for (String p :r) {
            if (!p.startsWith("/")) {
                tmp.add("/" + p);
            } else {
                tmp.add(p);
            }
        }
        return ListUtils.joining(tmp,"","{Path: ", "}");
    }

}
